package com.arch.bigdata.hbase1x.lx;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.CellScanner;
import org.apache.hadoop.hbase.CellUtil;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;
import org.apache.hadoop.hbase.client.HBaseAdmin;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.client.ResultScanner;
import org.apache.hadoop.hbase.client.Scan;
import org.apache.hadoop.hbase.client.Table;
import org.apache.hadoop.hbase.filter.FirstKeyOnlyFilter;
import org.apache.hadoop.hbase.filter.PageFilter;
import org.apache.hadoop.hbase.util.Bytes;

import java.io.IOException;
import java.util.List;

/**
 * @author pizhihui
 * @date 2020-08-24
 */
public class HbaseDemo {

    private static final LxRealTimeHbaseClient hbaseClient = new LxRealTimeHbaseClient("用户画像", "CLUSTER_KV");


    public static Configuration configuration;

    private static final String CLUSTER_ADDRESS_KV = "1.hadoop3.com:2181,2.hadoop3.com:2181,3.hadoop3.com:2181";


    static {
        configuration = HBaseConfiguration.create();
        configuration.set("hbase.zookeeper.quorum", CLUSTER_ADDRESS_KV);
        configuration.set("hbase.zookeeper.property.clientPort", "2181");
        configuration.set("hadoop.job.ugi", "biadmin");
        configuration.set("hbase.client.userprovider", "biadmin");

        hbaseClient.addHbaseConfig("hbase.rpc.timeout", "2500");
        System.out.println("静态代码块");
    }

    public static boolean isTableExists(String tableName) throws IOException {
        Connection connection = ConnectionFactory.createConnection(configuration);
        HBaseAdmin admin = (HBaseAdmin) connection.getAdmin();
        return admin.tableExists(TableName.valueOf(tableName));

    }

    public static void getCol(String tableName) throws IOException {
        Connection connection = ConnectionFactory.createConnection(configuration);
        HBaseAdmin admin = (HBaseAdmin) connection.getAdmin();
        HTableDescriptor tableDescriptor = admin.getTableDescriptor(TableName.valueOf(tableName));

        HColumnDescriptor[] columnFamilies = tableDescriptor.getColumnFamilies();
        for (HColumnDescriptor columnFamily : columnFamilies) {
            System.out.println(columnFamily.getNameAsString());
        }
    }

    public static void getKey(String tableName)throws IOException {
        Connection connection = ConnectionFactory.createConnection(configuration);

        Table table = connection.getTable(TableName.valueOf(tableName));
        Scan scan = new Scan();
        scan.setFilter(new FirstKeyOnlyFilter());
        scan.setFilter(new PageFilter(1));
        ResultScanner scanner = table.getScanner(scan);
        for (Result rr : scanner) {
            byte[] key = rr.getRow();
            for (Cell cell : rr.listCells()) {
                String qualifier = Bytes.toString(CellUtil.cloneQualifier(cell));
                String cf = Bytes.toString(CellUtil.cloneFamily(cell));
                System.out.println(cf + ":::" + qualifier);
            }
            System.out.println(Bytes.toString(key));
        }

    }


    public static void main(String[] args) throws IOException {

//        System.out.println(isTableExists("all_user_label_info_no_compress_split_fql_v2"));
        getCol("t_relation_network_entity_risk_mac");
        System.out.println("=====================");

        getKey("t_relation_network_entity_risk_mac");

        System.out.println("end........");
    }

}
